import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import formBindingData from '@ohos.app.form.formBindingData';
import formProvider from '@ohos.app.form.formProvider';
import formInfo from '@ohos.app.form.formInfo';

export default class EntryAbility1 extends FormExtensionAbility {
  onAddForm(want) {
    console.log('FormExtensionAbility onAddForm, want:' + want.abilityName);
    let dataObj1 = {
      temperature: '11c',
      'time': '11:00'
    };
    let obj1 = formBindingData.createFormBindingData(dataObj1);
    return obj1;
  }

  onCastToNormalForm(formId) {
    console.log('FormExtensionAbility onCastToNormalForm, formId:' + formId);
  }

  onUpdateForm(formId) {
    console.log('FormExtensionAbility onUpdateForm, formId: ${formId}');
    let obj2 = formBindingData.createFormBindingData({
      temperature: '22c',
      time: '22:00'
    });
    formProvider.updateForm(formId, obj2).then((data) => {
      console.log('FormExtensionAbility context updateForm, data: ${data}');
    }).catch((error) => {
      console.error('Operation updateForm failed. Cause: ${error}');
    });
  }

  onChangeFormVisibility(newStatus) {
    console.log('FormExtensionAbility onChangeFormVisibility, newStatus: ${newStatus}');
    let obj2 = formBindingData.createFormBindingData({
      temperature: '22c',
      time: '22:00'
    });

    for (let key in newStatus) {
      console.log('FormExtensionAbility onChangeFormVisibility, key: ${key}, value= ${newStatus[key]}');
      formProvider.updateForm(key, obj2).then((data) => {
        console.log('FormExtensionAbility context updateForm, data: ${data}');
      }).catch((error) => {
        console.error('Operation updateForm failed. Cause: ${error}');
      });
    }
  }

  onFormEvent(formId, message) {
    console.log('FormExtensionAbility onFormEvent, formId:' + formId + ', message:' + message);
  }

  onRemoveForm(formId) {
    console.log('FormExtensionAbility onRemoveForm, formId:' + formId);
  }

  onConfigurationUpdate(config) {
    console.log('onConfigurationUpdate, config:' + JSON.stringify(config));
  }

  onAcquireFormState(want) {
    console.log('FormExtensionAbility onAcquireFormState, want:' + want);
    return formInfo.FormState.UNKNOWN;
  }
}
